package com.lijie.ljblog.controller;

import com.lijie.ljblog.model.Game;
import com.lijie.ljblog.model.dto.GameScoreDTO;
import com.lijie.ljblog.model.dto.GameScoreSaveDTO;
import com.lijie.ljblog.model.vo.GameScoreVO;
import com.lijie.ljblog.service.GameCalculateContext;
import com.lijie.ljblog.service.GameService;
import com.lijie.ljblog.service.impl.GameServiceImpl;
import com.lijie.ljblog.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @Author lijie
 * @Date 2022/8/24 14:12
 **/
@RestController
@RequestMapping("/game")
public class GameController {

    @Resource
    GameServiceImpl gameService;

    @PostMapping("getGameList")
    public Result<List<Game>> getGameList(){
        return Result.ok(gameService.list());
    }
    @PostMapping("getGameSaveOrUpdate")
    public  Result<Object> getGameSaveOrUpdate(@RequestBody Game game){
        return Result.ok(gameService.saveOrUpdate(game));
    }
    @GetMapping("getGameDelete/{id}")
    public Result<Object> getGameDelete(@PathVariable Integer id){
        return Result.ok(gameService.removeById(id));
    }

    @ApiOperation("获得用户游戏最好成绩")
    @PostMapping("getPersonalGameScore")
    public  Result<Object> getGameScore(@RequestBody GameScoreDTO gameScoreDTO){
        GameService gameService = GameCalculateContext.getGameService(gameScoreDTO.getGameName());
        if (gameService==null){
            throw new RuntimeException("无游戏："+gameScoreDTO.getGameName());
        }
        return Result.ok(gameService.getGameScore(gameScoreDTO));
    }

    @ApiOperation("过关后，保存成绩数据")
    @PostMapping("saveGameScore")
    public  Result<Object> saveGameScore(@RequestBody GameScoreSaveDTO gameScoreSaveDTO){
        GameService gameService = GameCalculateContext.getGameService(gameScoreSaveDTO.getGameName());
        if (gameService==null){
            throw new RuntimeException("无游戏："+gameScoreSaveDTO.getGameName());
        }
        return Result.ok(gameService.saveGameScore(gameScoreSaveDTO));
    }
    @ApiOperation("获得游戏分数排名")
    @PostMapping("getGameScoreRank")
    public  Result<Object> getGameScoreRank(@RequestBody GameScoreDTO gameScoreDTO){
        GameService gameService = GameCalculateContext.getGameService(gameScoreDTO.getGameName());
        if (gameService==null){
            throw new RuntimeException("无游戏："+gameScoreDTO.getGameName());
        }
        return Result.ok(gameService.getGameScoreRank(gameScoreDTO));
    }


}
